package com.birdboot.controller;

import com.birdboot.annotation.Controller;
import com.birdboot.annotation.RequestMapping;
import com.birdboot.entity.User;
import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;
import com.birdboot.utils.DBUtil;

import java.io.*;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 用于处理与用户数据相关业务的类
 */
@Controller
public class UserController {
    private static File userDir;
    static{
        userDir = new File("./users");
        if(!userDir.exists()){
            userDir.mkdirs();
        }
    }

    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理用户注册");
        //1获取表单数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+ageStr);
        if(username==null||username.isEmpty()||
           password==null||password.isEmpty()||
           nickname==null||nickname.isEmpty()||
           ageStr==null||ageStr.isEmpty()||
           !ageStr.matches("[0-9]{1,3}")){
            //重定向
            response.sendRedirect("/reg_info_error.html");
            return;
        }
        int age = Integer.parseInt(ageStr);

        //2.处理注册逻辑[数据库]
        try (Connection connection = DBUtil.getConnection();){
            //查询语句:用于确认用户名是否被占用
            String sql1 = "SELECT id FROM userinfo WHERE username=?";
            PreparedStatement ps1 = connection.prepareStatement(sql1);
            ps1.setString(1, username);
            ResultSet rs1 = ps1.executeQuery();
            if (rs1.next()){ //被占用
                System.out.println("用户名已被占用");
                response.sendRedirect("/have_user.html");
            }else{//执行注册流程
                String sql2 = "INSERT INTO userinfo(username,password,nickname,age) VALUES(?,?,?,?)";
                PreparedStatement ps2 = connection.prepareStatement(sql2);
                ps2.setString(1, username);
                ps2.setString(2, password);
                ps2.setString(3, nickname);
                ps2.setInt(4, age);
                int i = ps2.executeUpdate();
                System.out.println("注册成功");
                response.sendRedirect("/reg_success.html");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用户登录!!!!");

        //1获取表单信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //必要的验证工作
        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
            response.sendRedirect("/login_info_error.html");
            return;
        }
        //处理登录流程[数据库]
        //JDBCLogin2中的代码
        try (Connection connection = DBUtil.getConnection();){
            /*
                1.创建预编译SQL执行对象
                2.设置 ? 的值
                3.执行SQL语句
                4.处理查询结果
             */
            String sql = "SELECT nickname FROM userinfo WHERE username=? AND password=?";
            //注意:参数为预编译SQL语句
            PreparedStatement ps1 = connection.prepareStatement(sql);
            ps1.setString(1, username);
            ps1.setString(2, password);
            //注意:执行方法中不需要再添加参数
            ResultSet rs1 = ps1.executeQuery();

            //处理查询结果
            if (rs1.next()){
                String nickname = rs1.getString("nickname");
                System.out.println("欢迎你:" + nickname);
                //跳转到登录成功页面
                response.sendRedirect("/login_success.html");
            }else{
                System.out.println("用户名或密码错误");
                //跳转到登录失败页面
                response.sendRedirect("/login_fail.html");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
